/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.dao;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;

import com.tech15.core.model.ModuleFunction;
import com.tech15.core.vo.query.ModuleFunctionQuery;

@Repository
public class ModuleFunctionDao extends BaseHibernateDao<ModuleFunction,Long>{

	public Class getEntityClass() {
		return ModuleFunction.class;
	}
	
	public Page findPage(ModuleFunctionQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
		String sql = "select t from ModuleFunction t where 1=1 "
			  	+ "/~ and t.name = {name} ~/"
			  	+ "/~ and t.url = {url} ~/"
			  	+ "/~ and t.remark = {remark} ~/"
			  	+ "/~ and t.type = {type} ~/"
			  	+ "/~ and t.mdid = {mdid} ~/"
				+ "/~ order by [sortColumns] ~/";

		return pageQuery(sql,query);
	}
	/**
	 * 判断是否拥有此功能
	 * @param userid
	 * @param url
	 * @return
	 */
	public Integer isHasFunction(Long userid,String url){
		String sql = "select count(*) from ("+
					"select t.* from sys_module_function t,sys_module_auth t1 where t.mdid=t1.mdid and t1.auth_type=1 and t1.authid="+userid+
					" union "+
					"select t.* from sys_module_function t,sys_module_auth t1,sys_group_user t2 where t.mdid=t1.mdid and t1.authid=t2.pgid and t1.auth_type=2 and t2.userid="+userid+ 
					" union "+
					"select t.* from sys_module_function t where t.type=1"+
					") t1 where t1.url='"+url+"'";
		return getCount(sql);
	}
	/**
	 * 判断权限管理员是否拥有此功能
	 * @param userid
	 * @param url
	 * @return
	 */
	public Integer isHasAdminFunction(String url){
		String sql = "select count(*) from "
					 +" (select t.* from sys_module_function t where t.type=1) t1"
					 +" where t1.url='"+url+"'";
		return getCount(sql);
	}

}
